package com.tz.cartelo.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tz.cartelo.R;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.GoodsSubclass;
import com.tz.cartelo.result.Result;
import com.tz.cartelo.result.ResultMyOrderDetails;
import com.tz.cartelo.util.AES;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * Created by Administrator on 2017/4/16.
 */

public class MyOrderDetailsActivity extends DataLoadActivity implements View.OnClickListener {
    private static final String TAG = "MyOrderDetailsActivity";
    private Api mApi;
    private LinearLayout my_order_goods_style_layout;
    private TextView my_order_num, my_order_no, my_order_time;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_order_details;
    }

    @Override
    protected void init() {

        my_order_goods_style_layout = (LinearLayout) findViewById(R.id.my_order_goods_style_layout);
        my_order_num = (TextView) findViewById(R.id.my_order_num);
        my_order_no = (TextView) findViewById(R.id.my_order_no);
        my_order_time = (TextView) findViewById(R.id.my_order_time);


        mApi = Api.MY_ORDER_DETAILS;
        loadData(mApi, true);
    }

    @Override
    protected void initTitle() {
        ImageView titleBack = (ImageView) findViewById(R.id.title_back);
        titleBack.setVisibility(View.VISIBLE);
        titleBack.setOnClickListener(this);

        TextView titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(R.string.order_details_title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case MY_ORDER_DETAILS:
                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                params.addParam("orderNo", getIntent().getStringExtra(Key.ORDER_NO));
                String time = System.currentTimeMillis() + "";
                params.addParam("time", time);
                List<String> dataList = new ArrayList<>();
                dataList.add(PreferencesUtils.getString(Key.PER_USER_ID, ""));
                dataList.add(getIntent().getStringExtra(Key.ORDER_NO));
                dataList.add(time);
                try {
                    params.addParam("sign", AES.createASE(dataList));
                } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case MY_ORDER_DETAILS:
                ResultMyOrderDetails resultMyOrderDetails = fromJson(response, ResultMyOrderDetails.class);
                if (!resultMyOrderDetails.isSuccess()) {
                    showToast(resultMyOrderDetails.getMessage());
                    return;
                }

                my_order_num.setText("下单总数：" + resultMyOrderDetails.getMyOrderDetails().getTotalNum());
                my_order_no.setText("订单编号：" + resultMyOrderDetails.getMyOrderDetails().getOrderNo());
                my_order_time.setText("下单时间：" + resultMyOrderDetails.getMyOrderDetails().getDate());

                int groupType = 1;
                for (GoodsSubclass goodsSubclass : resultMyOrderDetails.getMyOrderDetails().getGoodsSubclassList()) {
                    View goodsStyleView = LayoutInflater.from(this).inflate(R.layout.item_goods_style, null);
                    TextView goodsStyleDescribe = (TextView) goodsStyleView.findViewById(R.id.goods_style_describe);
                    TextView goodsStyleStock = (TextView) goodsStyleView.findViewById(R.id.goods_style_stock);
                    goodsStyleDescribe.setText(goodsSubclass.getAppearance());
                    goodsStyleStock.setText(goodsSubclass.getNum());
                    goodsStyleStock.setTextColor(getResources().getColor(R.color.black));
                    if (groupType != Integer.parseInt(goodsSubclass.getGroupType())) {
                        View grayLineView = LayoutInflater.from(this).inflate(R.layout.item_gray_line, null);
                        my_order_goods_style_layout.addView(grayLineView);
                        groupType++;
                    }
                    my_order_goods_style_layout.addView(goodsStyleView);
                }

                break;
            default:
                break;
        }
    }


}
